package com.alex.maze.entities;

import java.util.ArrayList;
import java.util.List;

public class Player extends Entity
{
    public Player()
    {
        r = 0.3;
    }
    public void azuriraj(boolean upKey, boolean downKey, boolean leftKey, boolean rightKey, boolean turnleft, boolean turnRight, boolean runKey)
    {
        double rotSpeed = 0.05;
        double brzinaHodanja = 0.03;
        
        if (turnleft)
        {
            rota += rotSpeed; // Hmm da li mi je ceo Bit3D inverovan?
        }
        if (turnRight)
        {
            rota -= rotSpeed; // Hmm da li mi je ceo Bit3D inverovan?
        }
        
        double xm = 0;
        double zm = 0;
        
        if (runKey) // TRCANJE NE RADI VISE!!! Double dd ga je ubio! Zato ako ides ukos vec trcis sto znaci nova implementacija za hodanje i trcanje mora da bidne!
        {
            zm -= 0.6;
        }
        if (upKey)
        {
            zm--;
        }
        if (downKey)
        {
            zm++;
        }
        if (leftKey)
        {
            xm--;
        }
        if (rightKey)
        {
            xm++;
        }
        
        double dd = xm * xm + zm * zm;
        if (dd > 0)
        {
            dd = Math.sqrt(dd);
        }
        else
        {
            dd = 1;
        }
        
        xm /= dd;
        zm /= dd;
        
        xa -= (xm * Math.cos(rot) + zm * Math.sin(rot)) * brzinaHodanja; // Hmm da li mi je ceo Bit3D inverovan?
        za -= (zm * Math.cos(rot) - xm * Math.sin(rot)) * brzinaHodanja; // Hmm da li mi je ceo Bit3D inverovan?
        
        move();
        
        xa *= 0.6;
        za *= 0.6;
        
        rot += rota;
        rota *= 0.4;
    }
    
    public void activate()
    {
        double xa = (1 * Math.sin(rot));  // * 10; // Hmm da li mi je ceo Bit3D inverovan?
        double za = (1 * Math.cos(rot));  // * 10; // Hmm da li mi je ceo Bit3D inverovan?
        
        int rr = 2;
        
        int xc = (int) (Math.floor(x + 0.5));
        int zc = (int) (Math.floor(z + 0.5));
        
        List<Entity> possibleHits = new ArrayList<Entity>();
        
        for (int y = zc - rr; y <= zc + rr; y++)
        {
            for (int x = xc - rr; x <= xc + rr; x++)
            {
                List<Entity> es = nivo.getBlock(x, y).entities;
                for (int i = 0; i < es.size(); i++)
                {
                    Entity e = es.get(i);
                    
                    if (e == this)
                    {
                        continue;
                    }
                    possibleHits.add(e);
                }
            }
        }
        
        int divs = 100;
        for (int i = 0; i < divs; i++)
        {
            double xx = x + xa * i / divs;
            double zz = z + za * i / divs;
            for (int j = 0; j < possibleHits.size(); j++)
            {
                Entity e = possibleHits.get(j);
                if (e.contains(xx, zz))
                {
                    if (e.use(this))
                    {
                        return;
                    }
                }
            }
            if (nivo.getBlock((int) (xx + 0.5), (int) (zz + 0.5)).use())
            {
                return;
            }
        }
    }
}
